<?php

namespace app\admin\controller\house;

use app\api\model\house\HouseBedInfo;

use app\api\model\house\HouseOrderComplainReply;
use app\common\controller\Backend;

/**
 * 房屋订单
 *
 * @icon fa fa-circle-o
 */
class Houseorder extends Backend
{

    /**
     * HouseOrder模型对象
     * @var \app\admin\model\house\HouseOrder
     */
    protected $model = null;
    protected $HouseOrderDateModel = null;
    protected $HouseOrderOccupantModel = null;
    protected $HouseOrderActionLogModel = null;
    protected $HouseOrderComplementModel = null;
    protected $HouseOrderComplain = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\house\HouseOrder;
        $this->HouseOrderDateModel = new \app\admin\model\house\HouseOrderDate;
        $this->HouseOrderOccupantModel = new \app\admin\model\house\HouseOrderOccupant();
        $this->HouseOrderActionLogModel = new \app\admin\model\house\HouseOrderActionLog();
        $this->HouseOrderComplementModel = new \app\admin\model\house\HouseOrderComplement();
        $this->HouseOrderComplain = new \app\admin\model\house\HouseOrderComplain();
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                    ->with(['user'])
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);

            foreach ($list as $row) {
                $row->visible(['order_id','user_id','house_id','house_title','order_type','order_status','owner_order_status','order_no','house_money','complement_money','order_money','coupon_money','pay_money','discount_money','checkin_date','leave_date','pay_type','pay_status','pay_time','days','buyer_message','createtime']);
                $row->visible(['user']);
				$row->getRelation('user')->visible(['nickname','avatar']);
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }


    //订单详情
    public function detail($ids = null)
    {
        $row = $this->model->get($ids);

        $this->view->assign("row", $row);

        //每日价格
        $date_list = $this->HouseOrderDateModel::where(['order_id'=>$ids])->select();
        foreach ($date_list as $k=>$v){
            if ($v['bed_id']){
                $bed_info = \app\admin\model\house\HouseBedInfo::get(['bed_id'=>$v['bed_id']]);
                $date_list[$k]['bed_name'] = $bed_info['bed_name'];
            } else {
                $date_list[$k]['bed_name'] = '';
            }
        }
        $this->view->assign("date_list", $date_list);

        //入住人
        $occupant_list = $this->HouseOrderOccupantModel::where(['order_id'=>$ids])->select();
        foreach ($occupant_list as $k=>$v){
            if ($v['bed_id']){
                $bed_info = \app\admin\model\house\HouseBedInfo::get(['bed_id'=>$v['bed_id']]);
                $occupant_list[$k]['bed_name'] = $bed_info['bed_name'];
            } else {
                $occupant_list[$k]['bed_name'] = '';
            }
        }

        $this->view->assign("occupant_list", $occupant_list);

        //拼房的卧室
        $bed_name = '';
        if ($row->bed_id){
            $bed_list = HouseBedInfo::where('bed_id','in',$row->bed_id)->column('bed_name');
            $bed_name = implode(',',$bed_list);
        }
        $this->view->assign("bed_name", $bed_name);

        //操作记录
        $action_log  = $this->HouseOrderActionLogModel::where(['order_id'=>$ids])->select();
        $this->view->assign("action_log", $action_log);

        //配套服务
        $complement = $this->HouseOrderComplementModel::where(['order_id'=>$ids])->select();
        $this->view->assign("complement", $complement);

        //投诉
        $dispose_list = '';
        $complain = $this->HouseOrderComplain::where(['order_id'=>$ids])->find();
        if ($complain&&isset($complain['images'])){
            $complain['images'] = explode(',',$complain['images']);
        }
        $this->view->assign("complain", $complain);
        if ($complain){
            $dispose_list = HouseOrderComplainReply::where(['complain_id'=>$complain['complain_id']])
                ->order('createtime asc')
                ->select();
            foreach ($dispose_list as $k=>$v){
                if ($v['images']){
                    $dispose_list[$k]['images'] = explode(',',$v['images']);
                }

            }
        }
        $this->view->assign("dispose_list", $dispose_list);

        return $this->view->fetch();
    }
}
